package com.platform.qujia.utils;

import lombok.Getter;
import lombok.Setter;

/**
 * @date 2023/9/30
 */
@Setter
@Getter
public class BGResult<T> {

    public static final String SUCCESS_MSG = "操作成功";
    public static final int SUCCESS_CODE = 200;
    public static final int DEFAULT_ERROR_CODE = 500;

    private String msg;
    private int code;
    private T data;

    /**
     * 成功时候的调用
     * @return
     */
    public static <T> BGResult<T> success(T data){
        BGResult<T> r = new BGResult<>();
        r.code = SUCCESS_CODE;
        r.msg = SUCCESS_MSG;
        r.data = data;
        return r;
    }

    /**
     * 成功，不需要传入参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public static BGResult success(){
        return success(null);
    }

    /**
     * 失败时候的调用
     * @return
     */
    public static BGResult error(String msg){
        return error(DEFAULT_ERROR_CODE,msg);
    }
    /**
     * 失败时候的调用
     * @return
     */
    public static BGResult error(int code,String msg){
        return error(code,msg,null);
    }
    /**
     * 失败时候的调用,扩展消息参数
     * @return
     */
    public static <T> BGResult<T> error(int code,String msg,T data){
        BGResult<T> r = new BGResult<>();
        r.code = code;
        r.msg = msg;
        r.data = data;
        return r;
    }
}
